<#assign baseURL = springMacroRequestContext.getContextPath() />
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拓深科技</title>
<#include "./css.ftl">
    <style type="text/css">
        .mapBall{
            width:20px;
            height:20px;
            background: red;
            border-radius: 50%;
            position: absolute;
        }
        .changeColor a:hover{
            color: white;
            cursor:pointer;
        }
    </style>
</head>
<body>
<div id="wrapper">

<#include "./left_nav.ftl">

    <div id="page-wrapper" class="gray-bg dashbard-1 animated fadeInRight">
    <#include "./top_nav.ftl">
        <div class="wrapper wrapper-content">
            <div class="row">
                <div class="col-md-3">
                    <div class="box box-warning">
                        <div class="box-header">
                            <h4 class="box-title"><b>当前区域数据</b></h4>
                        </div>
                        <div class="box-body">
                            <pre class="prettyprint" style="border: none;height: 650px;">
                                <div id="loction_tree"></div>
                            </pre>
                        </div>
                    </div>
                </div>
                <div class="col-md-9">
                    <div class="box box-warning box-solid" style="margin-bottom: 0px">
                        <div class="box-header">
                            <h4 id="locations"></h4>
                        </div>
                        <div class="box-body">
                            <div class="row" id="area_display">

                                <div class="col-sm-12" id="build_map_div">
                                    <div class="col-lg-4" style="margin-left: 0px;padding: 0px 5px 5px 0px">
                                        <select id="build_select" class="form-control select2 build_select_class" style="width: 100%;"></select>
                                    </div>
                                    <div class="col-sm-2">
                                        <button class="btn btn-block btn-info" id="save_x_y" <#if (role_type_id > 2)>disabled</#if> ><i class="fa fa-save"></i>&nbsp;保存</button>
                                    </div>
                                    <div class="col-sm-2">
                                        <input style="display: none;" type="text" id="map_pos_x">
                                    </div>
                                    <div class="col-sm-2">
                                        <input style="display: none;" type="text" id="map_pos_y">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <section class="invoice">
                        <div style="border: none;height: 700px;">
                            <div id="iPicture" data-interaction="hover" style="width: 100%;height: 100%">
                                <div class="ip_slide" id="map_div" style="width: 100%;height: 100%">
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    <#include "./bottom_nav.ftl">
    </div>

</div>


<#include "./js.ftl">
<script src="assets/js/jquery.cxselect.min.js"></script>
<script>


    var server = "http://"+getURL()+"${baseURL}";
    var mapId = getParam('mapId') ;
    var mapAreaId = 0;
    var locationObjArr;
    var mapKeyValue = [];
    var hotSpotArr = [];
    var ignoreAlarmLocalId = new Array();
    $(function () {
        socketInit(server.substring(5));
        getIgnoreAlarm();
        dataInit();
        treeinit();
        if(mapId){
            var name= getParam('name');
            sikp(mapId,name);
        }else {
            mapInit();
        }
//        layer.msg("选择要标记的楼宇，描点后保存");
    });
    function dataInit() {
        $('#locations').empty();
        $('#map_div').empty();
        $.ajax({
            url: "${baseURL}/xkGeoLocation/getTree",
            async:false,
            success:function (data_in) {
                var jsonObj = data_in;
                var data = [];
                var ii = 0;
                for(var i  in jsonObj.data){
                    var level1 = jsonObj.data;
                    if(i==0){
                        var level1Obj = {
                            text:level1[i].name,
                            id:level1[i].id,
                            value:ii++,
                            level:1,
                            parentId:0,
                            icon:"glyphicon glyphicon-globe text-yellow",
                            state: {"opened" : true }
                        };

                    }else {
                        var level1Obj = {
                            text:level1[i].name,
                            id:level1[i].id,
                            value:ii++,
                            level:1,
                            parentId:0,
                            icon:"glyphicon glyphicon-globe text-yellow",
                            state: {"opened" : false }
                        };
                    }
                    if(level1[i].picture ==""){
                        level1Obj.text = level1[i].name + "(无地图)";
                    }
                    if(level1[i].children != null)
                    {
                        var level2 = level1[i].children;
                        var level2Arr = [];
                        var jj = 0;
                        for(var j in level2){
                            if(i==0 && j==0){
                                var level2Obj = {
                                    text: level2[j].name,
                                    id: level2[j].id,
                                    value:jj++,
                                    parentId:level1[i].id,
                                    posX:level2[j].posX,
                                    posY:level2[j].posY,
                                    level:2,
                                    icon: "glyphicon glyphicon-home text-yellow",
                                    state: {"opened" : true }
                                };
                            }else {
                                var level2Obj = {
                                    text: level2[j].name,
                                    id: level2[j].id,
                                    value:jj++,
                                    parentId:level1[i].id,
                                    posX:level2[j].posX,
                                    posY:level2[j].posY,
                                    level:2,
                                    icon: "glyphicon glyphicon-home text-yellow",
                                    state: {"opened" : false }
                                };
                            }
                            if(level2[j].picture ==""){
                                level2Obj.text = level2[j].name + "(无地图)";
                            }
                            if(level2[j].children != null)
                            {
                                var level3 = level2[j].children;
                                var level3Arr = [];
                                for(var k in level3){
                                    var level3Obj = {
                                        text: level3[k].name,
                                        id: level3[k].id,
                                        level:3,
                                        parentId:level2[j].id,
                                        icon: "glyphicon glyphicon-flag text-yellow"
                                    };
                                    if(level3[k].picture ==""){
                                        level3Obj.text = level3[k].name + "(无地图)";
                                    }
                                    level3Arr.push(level3Obj);
                                }
                                level2Obj .children = level3Arr;
                            }
                            level2Arr.push(level2Obj);
                        }
                        level1Obj.children = level2Arr;
                    }
                    data.push(level1Obj);
                }
                locationObjArr = data;
            }
        });
        $('#build_map_div').cxSelect({
            selects:['build_select_class'],
            jsonName:'text',
            jsonValue:'value',
            data:locationObjArr[mapAreaId].children
        });

    }
    function treeinit(){
        $('#loction_tree').data('jstree',false).empty();
        $('#loction_tree').jstree({
            'core' : {
                'data' : locationObjArr
            }
        });
    }
    function mapInit(){
        mapAreaId = locationObjArr[0].value;
        mapId = locationObjArr[0].id;
        var imgs = "";
        imgs = '<img id="show_map" style="width: 100%;height: 100%" class="ip_tooltipImg" src="${baseURL}/xkGeoLocationMap/mapDownload?id='+mapId+'" />';
        $('#map_div ').append(imgs);
        var span = "";
        span =locationObjArr[0].text;
        span = "<span>" +"当前位置："+ span + "</span>";
        $('#locations').append(span);
        var area_display = document.getElementById("area_display");
        area_display.style.display = "";
    }
    $('#loction_tree').on('select_node.jstree', function (e, data) {
        mapId = data.node.id;
        $('#map_div').empty();
        $('#locations').empty();
        var imgs = "";
        imgs = '<img id="show_map" style="width: 100%;height: 100%" class="ip_tooltipImg"  src="${baseURL}/xkGeoLocationMap/mapDownload?id='+mapId+'" />';
        $('#map_div').append(imgs);

        var span = "";
        span =data.node.original.text;
        span = "<span>" +"当前位置："+ span + "</span>";
        $('#locations').append(span);

        level = data.node.original.level;
        if(level == 1){
            mapAreaId = data.node.original.value;
            console.log(mapAreaId);
            var area_display = document.getElementById("area_display");
            area_display.style.display = "";
//            layer.msg("选择要标记的楼宇，描点后保存");
            $('#build_map_div').cxSelect({
                selects:['build_select_class'],
                jsonName:'text',
                jsonValue:'value',
                data:locationObjArr[mapAreaId].children
            });
        }else if(level == 2){
            var area_display = document.getElementById("area_display");
            area_display.style.display = "none";

        }else if(level ==3){
            var area_display = document.getElementById("area_display");
            area_display.style.display = "none";
            setTimeout(" map_floor_init(mapId)",1000);
        }
    });
    $('#build_select').on('change',function (e) {
        var mapbuildId = this.value;
        if(mapbuildId == ''){
            $('#map_div div').empty();
        }else {
            map_init(mapbuildId);
        }
    });
    function map_init(mapbuildId){
        var buildFlag;
        var floorFlag;
        for(var build_j in ignoreAlarmLocalId){
            if(locationObjArr[mapAreaId].children[mapbuildId].id == ignoreAlarmLocalId[build_j].build){
                buildFlag = 1;
                break;
            }
        }
        if(buildFlag){
            var nameSelected = locationObjArr[mapAreaId].children[mapbuildId].text;
            $('#map_pos_x').val(locationObjArr[mapAreaId].children[mapbuildId].posX);
            $('#map_pos_y').val(locationObjArr[mapAreaId].children[mapbuildId].posY);
            $('#map_div').empty();
            var imgs = "";
            imgs = '<img id="show_map" style="width: 100%;height: 100%"  class="ip_tooltipImg" src="${baseURL}/xkGeoLocationMap/mapDownload?id='+mapId+'" onclick="mapOnClick(event)" />';
            $('#map_div').append(imgs);
            var x = (parseFloat(locationObjArr[mapAreaId].children[mapbuildId].posX) * 100);
            x = x>0?x+'%':0;
            var y = (parseFloat(locationObjArr[mapAreaId].children[mapbuildId].posY) * 100);
            y = y>0?y+'%':0;
            var divs ="";
            divs ='<div class="ip_tooltip ip_img32" style="top: '+y+'; left: '+x+';" data-tooltipbg="bgwhite" data-round="roundBgR" data-animationtype="btt-slide">' +
                    '<div class="row" style="height: auto;width: 100px;margin: auto">' +
                    '   <div class="box box-primary box-solid">' +
                    '       <div class="box-header"><h4>'+nameSelected+'</h4></div>' +
                    '       <div class="box-body">' +
                    '           <table class="table table-bordered table-striped">' +
                    '               <tbody id="'+nameSelected+'">' +
                    '               </tbody>' +
                    '           </table>' +
                    '       </div>' +
                    '       </div>' +
                    '   </div>' +
                    '</div>';
            $('#map_div').append(divs);
            $( "#iPicture" ).iPicture();
            var tr ="";
            for(var i in locationObjArr[mapAreaId].children[mapbuildId].children){
                floorFlag = 0;
                for(var floor_id in ignoreAlarmLocalId){
                    if(locationObjArr[mapAreaId].children[mapbuildId].children[i].id == ignoreAlarmLocalId[floor_id].floor){
                        floorFlag = 1;
                        break;
                    }
                }
                if(floorFlag){
                    tr += "<tr><td class='changeColor' style='border: solid;'><a onclick=sikp('"+locationObjArr[mapAreaId].children[mapbuildId].children[i].id+"','"+locationObjArr[mapAreaId].children[mapbuildId].children[i].text+"')><span style='color: red'>" + locationObjArr[mapAreaId].children[mapbuildId].children[i].text +"</span></a></td><tr>"
                }else {
                    tr += "<tr><td class='changeColor'><a onclick=sikp('"+locationObjArr[mapAreaId].children[mapbuildId].children[i].id+"','"+locationObjArr[mapAreaId].children[mapbuildId].children[i].text+"')>" + locationObjArr[mapAreaId].children[mapbuildId].children[i].text +"</a></td><tr>"
                }
            }
            $("#"+nameSelected).append(tr);
        }else{
            var nameSelected = locationObjArr[mapAreaId].children[mapbuildId].text;
            $('#map_pos_x').val(locationObjArr[mapAreaId].children[mapbuildId].posX);
            $('#map_pos_y').val(locationObjArr[mapAreaId].children[mapbuildId].posY);
            $('#map_div').empty();
            var imgs = "";
            imgs = '<img id="show_map" style="width: 100%;height: 100%"  class="ip_tooltipImg" src="${baseURL}/xkGeoLocationMap/mapDownload?id='+mapId+'" onclick="mapOnClick(event)" />';
            $('#map_div').append(imgs);
            var x = (parseFloat(locationObjArr[mapAreaId].children[mapbuildId].posX) * 100);
            x = x>0?x+'%':0;
            var y = (parseFloat(locationObjArr[mapAreaId].children[mapbuildId].posY) * 100);
            y = y>0?y+'%':0;
            var divs ="";
            divs ='<div class="ip_tooltip ip_img32" style="top: '+y+'; left: '+x+';" data-tooltipbg="bgwhite" data-round="roundBgG" data-animationtype="btt-slide">' +
                    '<div class="row" style="height: auto;width: 100px;margin: auto">' +
                    '   <div class="box box-primary box-solid">' +
                    '       <div class="box-header"><h4>'+nameSelected+'</h4></div>' +
                    '       <div class="box-body">' +
                    '           <table class="table table-bordered table-striped">' +
                    '               <tbody id="'+nameSelected+'">' +
                    '               </tbody>' +
                    '           </table>' +
                    '       </div>' +
                    '       </div>' +
                    '   </div>' +
                    '</div>';
            $('#map_div').append(divs);
            $( "#iPicture" ).iPicture();
            var tr ="";
            for(var i in locationObjArr[mapAreaId].children[mapbuildId].children){
                tr += "<tr><td class='changeColor'><a onclick=sikp('"+locationObjArr[mapAreaId].children[mapbuildId].children[i].id+"','"+locationObjArr[mapAreaId].children[mapbuildId].children[i].text+"')>" + locationObjArr[mapAreaId].children[mapbuildId].children[i].text +"</a></td><tr>"
            }
            $("#"+nameSelected).append(tr);
        }
    }
    function sikp(id,name){
        $('#map_div').empty();
        $('#locations').empty();
        var span = "";
        span =name;
        span = "<span>" +"当前位置："+ span + "</span>";
        $('#locations').append(span);

        var imgs = "";
        imgs = '<img id="show_map" style="width: 100%;height: 100%" class="ip_tooltipImg" src="${baseURL}/xkGeoLocationMap/mapDownload?id='+id+'" />';
        $('#map_div').append(imgs);
        var area_display = document.getElementById("area_display");
        area_display.style.display = "none";
        setTimeout(" map_floor_init('"+id+"')",1000);
    }
    $( "#iPicture" ).iPicture();
    $('#save_x_y').click(function(){
        var value = $('#build_select').val();
        var id = locationObjArr[mapAreaId].children[value].id;
        var parentId = locationObjArr[mapAreaId].children[value].parentId;
        var level=locationObjArr[mapAreaId].children[value].level;
        var name = locationObjArr[mapAreaId].children[value].text;
        var posX = $('#map_pos_x').val().toString();
        var posY = $('#map_pos_y').val().toString();
        console.log("parentId:"+parentId);
        console.log(mapAreaId);
        console.log(value);
        var formDatas = new FormData();
        formDatas.append("id",id);
        formDatas.append("name",name);
        formDatas.append("parent_id",parentId);
        formDatas.append("level",level);
        formDatas.append("pos_x",posX);
        formDatas.append("pos_y",posY);
        $.ajax({
            type:"post",
            contentType : false,
            processData:false,
            url: "${baseURL}/xkGeoLocation/update" ,
            data:formDatas,
            success:function (data) {
                if(data.code == 0){
                    dataInit();
                    map_init(value);
                }
                else{
                    layer.alert('操作失败：' + data,{
                        closeBtn: 0
                    });
                }
            }
        })
    })
    function getIgnoreAlarm(){
        $.ajax({
            url:"${baseURL}/alarm/getAllIgnore" ,
            async:false,
            success:function (data_in) {
                var json = data_in.data;
                for(var i in json){
                    var LocationId = {
                        id:json[i].id,
                        area:json[i].geo_1_id,
                        build: json[i].geo_2_id,
                        floor:json[i].geo_3_id,
                        monitor:json[i].monitor_id,
                    }
                    ignoreAlarmLocalId.push(LocationId);
                }
            }
        });
    }
    function map_floor_init(id){
        $('#map_div').find('div').each(function(){
            $(this).remove();
        });
        var w = $('#map_div').width();
        var h = $('#map_div').height();
        $.ajax({
            url:'${baseURL}/xkMonitor/getFloor?id='+id,
            async:false,
            success:function(data){
                var jsonObj = data.data;
                var monitorFlag;
                for(var i in jsonObj){
                    monitorFlag = 0;
                    if((!jsonObj[i].monitor_x)  && (!jsonObj[i].monitor_y)){
                        continue;
                    }
                    for(var monitor_i in ignoreAlarmLocalId){
                        if(jsonObj[i].id == ignoreAlarmLocalId[monitor_i].monitor){
                            monitorFlag=1;
                            break;
                        }
                    }
                    if(monitorFlag){
                        var x = parseFloat(jsonObj[i].monitor_x) * 100 ;
                        var y = parseFloat(jsonObj[i].monitor_y) * 100 ;
                        x = x>0?x+'%':0;
                        y = y>0?y+'%':0;
                        var divs = "";
                        divs ='<div class="ip_tooltip ip_img20" style="top: '+y+'; left: '+x+';z-index: 100;"  data-tooltipbg="bgblack" data-round="roundBgR" data-animationtype="btt-slide" >' +
                                '<p style="width: 100%;height: 100%" class="changeColor">名称：<a onclick="editRecord('+jsonObj[i].id+')" style="width: 100%;height: 100%">'+jsonObj[i].monitor_name+'</a></p>'+
                                '</div>';
                        $("#map_div").append(divs);
                    }else {
                        var x = parseFloat(jsonObj[i].monitor_x) * 100 ;
                        var y = parseFloat(jsonObj[i].monitor_y) * 100 ;
                        x = x>0?x+'%':0;
                        y = y>0?y+'%':0;
                        var divs = "";
                        divs ='<div class="ip_tooltip ip_img20" style="top: '+y+'; left: '+x+';z-index: 100;"  data-tooltipbg="bgblack" data-round="roundBgG" data-animationtype="btt-slide" >' +
                                '<p style="width: 100%;height: 100%" class="changeColor">名称：'+jsonObj[i].monitor_name+'</p>'+
                                '</div>';
                        $("#map_div").append(divs);
                    }

                }
                $( "#iPicture" ).iPicture();
            }
        })
    }
    function mapOnClick(e){
        e = e || window.event;
        var imgId ='#'+ $(e.target).attr('id');
        var currentWidth = $(imgId).width();
        var currentHeight = $(imgId).height();
        var offsetX = e.pageX - $(imgId).offset().left;
        var offsetY = e.pageY - $(imgId).offset().top;
        var x = offsetX / currentWidth;
        var y = offsetY / currentHeight;
        $('#map_pos_x').val(x.toFixed(10));
        $('#map_pos_y').val(y.toFixed(10));
        var w = $('#map_div').width();
        var h = $('#map_div').height();

        $("#map_div .ip_img20").remove();
        $("#map_div .ip_img32").remove();
        var spans = document.getElementsByClassName("mapBall");
        if(spans.length>0){
            $("#map_div .mapBall").remove();
        }
        console.log("w:"+w);
        console.log("h:"+h);
        console.log("x:"+x);
        console.log("y:"+y);
        posSetBall(w,h,x,y);
    }
    function posSetBall(w,h,x,y) {
        var curX = w * x;
        var curY = h * y;
        console.log("curX:"+curX);
        console.log("curY:"+curY);
        console.log($('#map_div').position().left);
        console.log($('#map_div').position().top);
        $('<span class="mapBall"></span>').css({
            width: 20,
            height: 20,
            left: curX-10 ,
            top: curY-10
        }).appendTo($('#map_div'));
    }
    function getParam(paramName) {
        var paramValue = "";
        var isFound = false;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            var arrSource = decodeURI(this.location.search).substring(1, this.location.search.length).split("&");
            var i = 0;
            while (i < arrSource.length && !isFound) {
                if (arrSource[i].indexOf("=") > 0) {
                    if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                        paramValue = arrSource[i].split("=")[1];
                        isFound = true;
                    }
                }
                i++;
            }
        }
        return paramValue;
    }

    function editRecord(monitor_id){
        layer.confirm("确认本监测点报警信息?",{
            btn:["确定","取消"]
        },function(){
            $.ajax({
                url:'${baseURL}/alarm/alarmUpdate?monitor_id='+ monitor_id,
                success:function (data) {
                    layer.msg('操作成功', {icon: 1});
                    location.reload();
                }
            });
        },function(){
        });
    }

</script>
</body>
</html>